package com.abss.vote.mapper;

import com.abss.vote.model.Table;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface TableMapper {

    @Select("select name from sysobjects where xtype = 'U'")
    List<String> selectTbale();

    @Select("select top 1 value from sys.extended_properties where major_id = object_id (#{tableName})")
    String selectChineseName(String tableName);

    @Select("SELECT \n" +
            "    表名       = case when a.colorder=1 then d.name else '' end,\n" +
            "    表说明     = case when a.colorder=1 then isnull(f.value,'') else '' end,\n" +
            "    字段序号   = a.colorder,\n" +
            "    字段名     = a.name,\n" +
            "    标识       = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,\n" +
            "    主键       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (\n" +
            "                     SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,\n" +
            "    类型       = b.name,\n" +
            "    占用字节数 = a.length,\n" +
            "    长度       = COLUMNPROPERTY(a.id,a.name,'PRECISION'),\n" +
            "    小数位数   = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),\n" +
            "    允许空     = case when a.isnullable=1 then '√'else '' end,\n" +
            "    默认值     = isnull(e.text,''),\n" +
            "    字段说明   = isnull(g.[value],'')\n" +
            "FROM \n" +
            "    syscolumns a\n" +
            "left join \n" +
            "    systypes b \n" +
            "on \n" +
            "    a.xusertype=b.xusertype\n" +
            "inner join \n" +
            "    sysobjects d \n" +
            "on \n" +
            "    a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'\n" +
            "left join \n" +
            "    syscomments e \n" +
            "on \n" +
            "    a.cdefault=e.id\n" +
            "left join \n" +
            "sys.extended_properties   g \n" +
            "on \n" +
            "    a.id=G.major_id and a.colid=g.minor_id  \n" +
            "left join\n" +
            "sys.extended_properties f\n" +
            "on \n" +
            "    d.id=f.major_id and f.minor_id=0\n" +
            "where \n" +
            "    d.name= #{tableName}   \n" + // --如果只查询指定表,加上此where条件，tablename是要查询的表名；去除where条件查询所有的表信息
            "order by \n" +
            "    a.id,a.colorder")
    @Results({
            @Result(property = "table_name", column = "表名"),
            @Result(property = "table_description", column = "表说明"),
            @Result(property = "number", column = "字段序号"),
            @Result(property = "colum_name", column = "字段名"),
            @Result(property = "identification", column = "标识"),
            @Result(property = "primary_key", column = "主键"),
            @Result(property = "type", column = "类型"),
            @Result(property = "length", column = "长度"),
            @Result(property = "byte_number", column = "占用字节数"),
            @Result(property = "digit", column = "小数位数"),
            @Result(property = "is_null", column = "允许空"),
            @Result(property = "default_value", column = "默认值"),
            @Result(property = "colum_description", column = "字段说明"),
            @Result(property = "number", column = "sex")
    })
    List<Table> selectInformation(String tableName);
}
